package com.sakuraio.nextj.core.holder;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;

/**
 * <p>SpringContextHolder</p>
 *
 * @author nekoimi 2023/03/09
 */
public class SpringContextHolder {
    private static boolean debug = false;
    private static ApplicationContext instance = null;

    public static ApplicationContext instance() {
        return instance;
    }

    public static boolean isDebug() {
        return debug;
    }

    public static <T> T getBean(Class<T> clazz) throws BeansException {
        Assert.notNull(instance(), "spring context is null");
        return instance().getBean(clazz);
    }

    public static void setInstance(ApplicationContext instance) {
        SpringContextHolder.instance = instance;
    }

    public static void setDebug(boolean debug) {
        SpringContextHolder.debug = debug;
    }
}
